/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.account;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.account.SysRolePermit;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-12-11
 */
@Service("sysrolepermitService")
public class SysRolePermitServiceImpl implements SysRolePermitService {
    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of SysRolePermit
     ****************************************/
    public void insert(SysRolePermit sysrolepermit) throws Exception {
        iDataSet4iBatis.insert(sysrolepermit);
    }

    /****************************************
     * Update a instace of SysRolePermit
     ****************************************/
    public void update(SysRolePermit sysrolepermit) throws Exception {
        iDataSet4iBatis.update(sysrolepermit);
    }

    /****************************************
     * Delete a instace of SysRolePermit
     ****************************************/
    public void delete(SysRolePermit sysrolepermit) throws Exception {
        iDataSet4iBatis.delete(sysrolepermit);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(List<?> sysrolepermits) throws Exception {
        List<BaseModel> sysrolepermitList = new ArrayList<BaseModel>();
        for (Object obj : sysrolepermits) {
            SysRolePermit sysRolePermit = new SysRolePermit();
            BeanUtils.populate(sysRolePermit, (Map<?, ?>) obj);
            sysRolePermit.setStatmentId("SysRolePermit.insert");
            sysrolepermitList.add(sysRolePermit);
        }
        iDataSet4iBatis.batchInsert(sysrolepermitList);
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(SysRolePermit sysrolepermit) throws Exception {
        String statementId = sysrolepermit.getStatmentId();
        String mainId = sysrolepermit.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> sysrolepermitList = new ArrayList<BaseModel>();
        for (String id : ids) {
            SysRolePermit model = new SysRolePermit();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            sysrolepermitList.add(model);
        }
        iDataSet4iBatis.batchDelete(sysrolepermitList);
    }

    /****************************************
     * Query instaces of SysRolePermit
     ****************************************/
    public List<BaseModel> queryAsList(SysRolePermit sysrolepermit) throws Exception {
        String queryListStatement = "";
        if (sysrolepermit.isPaging()) {
            queryListStatement = sysrolepermit.getStatmentId();
            sysrolepermit.setStatmentId("SysRolePermit.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(sysrolepermit);
            sysrolepermit.setTotalCount(baseModel.getTotalCount());
            sysrolepermit.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(sysrolepermit);
    }

    /****************************************
     * Query a instaces of SysRolePermit
     ****************************************/
    public BaseModel queryAsObject(SysRolePermit sysrolepermit) throws Exception {
        return iDataSet4iBatis.queryAsObject(sysrolepermit);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
